ControlInventario = function(){
	var configWindow = {
		add: "controlInventarioAgregarTop",
		edit: "controlInventarioEditarTop",
		save: "controlInventarioGuardarTop",
		remove: "controlInventarioEliminarTop",
		grid: "controlInventarioGrid",
		form: "controlInventarioForm"
	};
	var configWindowBottom = {
			add: "controlInventarioAgregarBottom",
			edit: "controlInventarioEditarBottom",
			save: "controlInventarioGuardarBottom",
			remove: "controlInventarioEliminarBottom"
	};
	return {
		agregarControlInventario: function(){
			Ext.getCmp("materiaPrimaId").allowEnable = false;
			Asi.form.switchButton(configWindow, "add");
			Asi.form.switchButton(configWindowBottom, "add");
//			Ext.getCmp("controlInventarioFechaNacimiento").allowEnable = true;
		},
		editarControlInventario: function(){
			Ext.getCmp("materiaPrimaId").allowEnable = false;
			Ext.getCmp("materiaPrimaIdDos").allowEnable = false;
			Asi.form.switchButton(configWindow, "edit");
			Asi.form.switchButton(configWindowBottom, "edit");
		},
		eliminarControlInventario: function(){
			Asi.message.confirmDelete(function(){
				Asi.message.progress("Eliminando Informaci\u00F3n...");
				Ext.Ajax.request({
					timeout: Asi.constants.TIMEOUT_SECONDS,
					url: Asi.constants.CONTEXT_PATH + "/controlInventario/delete",
					params: {
						controlInventarioId: Asi.utils.getValue("controlInventarioId")
					},
					callback: function(options, success, response){
						Ext.Msg.hide();
						if(success){
							var jsonObject = Asi.utils.ajaxRequestGetJson(response);
							if(jsonObject && jsonObject.success){
								Asi.form.switchButton(configWindow, "remove");
								Asi.form.switchButton(configWindowBottom, "remove");
								Asi.message.alert(jsonObject.message);
								Asi.form.clearAndDisable("controlInventarioForm");
								if(jsonObject.controlInventarios){
			    					Ext.getCmp("controlInventarioGrid").getStore().loadData(jsonObject.controlInventarios);
								}
								return;
							}
							jsonObject = jsonObject || {};
							Asi.message.alertInvalid(jsonObject.message || Asi.constants.DEFAULT_ERROR_MESSAGE);
							return;
						}
						Asi.message.alertInvalid(Asi.constants.DEFAULT_ERROR_MESSAGE);
					}
				});
			});
		},
		validacionIngresodeNuevoInventario: function(){
			var i = 0;
			var gridCompra = Ext.getCmp("controlInventarioGrid");
			var gridData = 0;
			Ext.each(gridCompra.getStore().getRange(), function(){
				if(Ext.getCmp("controlInventarioGrid").getStore().getRange()[i].data.materiaPrimaId == Ext.getCmp("materiaPrimaIdDos").getValue()){
					gridData = 1;
				}
				i++;
          });
			return gridData;
		},
		guardarControlInventario: function(){
			
			var i = 0;
			var gridCompra = Ext.getCmp("controlInventarioGrid");
			var gridData = 0;
			Ext.each(gridCompra.getStore().getRange(), function(){
				if(Ext.getCmp("controlInventarioGrid").getStore().getRange()[i].data.materiaPrimaId == Ext.getCmp("materiaPrimaIdDos").getValue()){
					gridData = 1;
				}
				i++;
          });
			
//			if(gridData == 0){
			
			Asi.message.progress("Guardando Informaci\u00F3n...");
			Ext.getCmp("controlInventarioForm").getForm().submit({
    			url: Asi.constants.CONTEXT_PATH + "/empleado/controlInventario/save",
    			timeout: Asi.constants.TIMEOUT_SECONDS,
    			params: {
    				cantidad: Asi.utils.getValue("inventarioCantidad"),
    				materiaPrimaId: Asi.utils.getValue("materiaPrimaIdDos"),
    				inventarioId: Ext.getCmp("inventariId").getValue()
    			},
    			success: function(form, action){
    				Asi.form.switchButton(configWindow, "save");
    				Asi.form.switchButton(configWindowBottom, "save");
    				Asi.message.alert(action.result.message);
    				Asi.form.setDisable("controlInventarioForm", true);
    				if(action.result.controlInventarios){
    					Ext.getCmp("controlInventarioGrid").getStore().loadData(action.result.controlInventarios);
    					Ext.getCmp("controlInventarioGrid").getSelectionModel().select(action.result.controlInventarioIndex);
    				}
    			},
    			failure: Asi.form.failureProcedure
   			});
			
			
			
//			} else {
//				Asi.message.alert("Ya Existe " + Ext.getCmp("materiaPrimaIdDos").displayTplData[0].materiaPrimaNombre + " en Inventario.");
//			}
			
		},
		init: function(config){
			var inventario = config.inventario;
			var configToReturn = {};
			var storeMaterial = new Ext.data.SimpleStore({
				data: config.ctgColor || [],
				fields: [
				         "ctgCatalogoId",
				         "ctgCatalogoNombre",
				         "ctgCatalogoPadre",
				         "ctgCatalogoHijo"
				        ]
			});
			configToReturn.items = [];
			configToReturn.menu = AsiBotones.getBotones(AsiGen.getTipoUsuario(), AsiGen.getPantallaId());
			configToReturn.items.push({
				flex: 1,
				title: "CONTROL DE INVENTARIO",
				autoScroll: true,
				xtype: "panel",
				layout: {
				    type: "vbox",
				    align : "center",
				    pack  : "start"
				},
				defaults: {width: 730, margins: "5 0 5 0"},
				dockedItems: [
					{
						
						xtype: "toolbar",
						dock: "top",
//						hidden: AsiKYC.getKycVigente() === false,
						items: [
						   '->',
						   {
							   text: "Nuevo",
							   id: "controlInventarioAgregarTop",
							   iconCls: Asi.constants.icons.ADD_ICON,
							   handler: ControlInventario.agregarControlInventario
						   },{
					    	   text: "Editar",
					    	   id: "controlInventarioEditarTop",
							   iconCls: Asi.constants.icons.EDIT_ICON,
							   handler: ControlInventario.editarControlInventario,
					       },{
					    	   text: "Guardar",
					    	   id: "controlInventarioGuardarTop",
							   iconCls: Asi.constants.icons.SAVE_ICON,
							   handler: ControlInventario.guardarControlInventario
					       },{
					    	   text: "Eliminar",
					    	   id: "controlInventarioEliminarTop",
							   iconCls: Asi.constants.icons.DELETE_ICON,
							   handler: ControlInventario.eliminarControlInventario
					       }
					    ]
					},{
						
			        	xtype: "toolbar",
			        	dock: "bottom",
//			        	hidden: AsiKYC.getKycVigente() === false,
			        	bodyStyle: "border: solid",
			        	items: [
			        	   '->',
			        	   {
							   text: "Nuevo",
							   id: "controlInventarioAgregarBottom",
							   iconCls: Asi.constants.icons.ADD_ICON,
							   handler: ControlInventario.agregarControlInventario
						   },{
					    	   text: "Editar",
					    	   id: "controlInventarioEditarBottom",
							   iconCls: Asi.constants.icons.EDIT_ICON,
							   handler: ControlInventario.editarControlInventario
					       },{
					    	   text: "Guardar",
					    	   id: "controlInventarioGuardarBottom",
							   iconCls: Asi.constants.icons.SAVE_ICON,
							   handler: ControlInventario.guardarControlInventario
					       },{
					    	   text: "Eliminar",
					    	   id: "controlInventarioEliminarBottom",
							   iconCls: Asi.constants.icons.DELETE_ICON,
							   handler: ControlInventario.eliminarControlInventario
					       }
		        	    ]
			        }
	            ],
				items: [
					{
						title: "CANTIDAD DE PRODUCTO",
						xtype: "grid",
						id: "controlInventarioGrid",
						height: 150,
						minHeight: 150,
						collapsible : true,
						store: new Ext.data.SimpleStore({
					    	data: inventario || [],
					    	fields: [
								"inventariId",
								"materiaPrimaIdDos",
								"inventarioCantidad",
								"materiaPrimaId",
								"materiaPrimaNombre"
			    	        ]
					    }),
					    columns: [
//					        {header: "Imagen", flex:1, minWidth: 120, dataIndex: "controlInventarioImage",
//					        	renderer: function(value){
//					                return '<img src="' + value + '" />';
//					            }
//					        },
							{header: "Id",  dataIndex: "materiaPrimaId", flex:1, width: 120},
				            {header: "Nombre",  dataIndex: "materiaPrimaNombre", flex:1, minWidth: 120},
				            {header: "Cantidad",  dataIndex: "inventarioCantidad", flex:1, width: 120}
				            

					    ],

					    listeners: {
					    	selectionchange: function(model, records){
					    		if(!records || records.length <= 0) return;
					    		var record = records[0];
					    		if(record != null){

					    			Asi.form.setValues("controlInventarioForm", record.data);
					    			Asi.form.setDisable("controlInventarioForm");
					    		}
					    		Asi.form.switchButton(configWindow, "rowclick");
					    		Asi.form.switchButton(configWindowBottom, "rowclick");
					    	},
					    	afterrender: function(){
					    		if(this.store.data.items.length > 0) Asi.grid.selectFirstRow(this);
					    		Asi.form.switchButton(configWindow, "cancelNotClear");
					    		Asi.form.switchButton(configWindowBottom, "cancelNotClear");
					    	}
					    }
					},{
			        	xtype: "form",
						id: "controlInventarioForm",
						flex: 1,
					    border: false,
					    autoScroll: true,
						layout: {
							type: "table",
							columns: 6,
							style : {
								width : "730px",
								"margin-top" : "5px",
								"margin-bottom" : "40px"
							},
							align : "center"
						},
						defaults: {
							width: 230,
							selectOnFocus: true,
							enforceMaxLength: true,
							maxLength: 200,
							typeAhead: true,
							minChars: 1,
							queryMode: "local",
							forceSelection: true,
							allowEnable: true,
							colspan: 2
						},
						items: [
				        	 {	
								xtype: "label",
								text: "CONTROL INVENTARIO",
								cls: "x-form-item label_header",
								colspan: 8,
								width: 730
							},
							{xtype: "label", text: "C\u00f3digo", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Nombre", cls: "x-form-item label_spacing"},
							{xtype: "label", text: "Cantidad", cls: "x-form-item label_spacing"},
							{
								
								xtype: "textfield",
								id: "materiaPrimaId",
								name: "controlInventario.materiaPrimaId",
								maxLength: 100
							},{
								xtype: "combo",
								id:"materiaPrimaIdDos",
								name: "controlInventario.materiaPrimaIdDos",
								store: new Ext.data.SimpleStore({
									data: config.materiaPrima || [],
									fields: ["materiaPrimaId", "materiaPrimaNombre"]
								}),
								displayField: "materiaPrimaNombre",
								valueField: "materiaPrimaId",
								colspan: 2,
								allowBlank: false
							},{
								xtype: "numberfield",
								id: "inventarioCantidad",
								name: "controlInventario.inventarioCantidad",
								allowDecimals: false,
								allowNegative: false,
								minValue: 1,
								allowBlank: false
							},{
								xtype: "hidden",
								id: "inventariId",
								name: "controlInventario.inventariId"
							}

						]
			        }
				]
				
			});
			return configToReturn;
		}
	};
}();